﻿/*
Copyright 2010 Matías Fidemraizer (www.matiasfidemraizer.com).

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

using System;
using Comkarl.Architecture.Patterns.DomainModel;

namespace Comkarl.Architecture.Patterns.UnitOfWork
{
    /// <summary>
    /// Represents a work object part of an unit of work instance.
    /// </summary>
    /// <typeparam name="TDomainObjectIdentifierName">
    /// The type for stored domain object' identifiers.
    /// </typeparam>
    /// <typeparam name="TDomainObject">
    /// The type for stored domain object.
    /// </typeparam>
    public sealed class WorkObject<TDomainObjectIdentifierName, TDomainObject> : DomainObject<TDomainObjectIdentifierName>
        where TDomainObjectIdentifierName : IEquatable<TDomainObjectIdentifierName>
        where TDomainObject : DomainObject<TDomainObjectIdentifierName>, ICloneable
    {
        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="domainObject">
        /// The associated domain object with this work.
        /// </param>
        public WorkObject(TDomainObject domainObject)
            : base(domainObject.ID)
        {
            this.domainObject = domainObject;
            this.dirtyDomainObject = (TDomainObject)domainObject.Clone();
        }

        /// <summary>
        /// Internal constructor used by System.
        /// </summary>
        /// <param name="domainObject">
        /// The associated domain object with this work.
        /// </param>
        /// <param name="isNew">
        /// A flag providing that the loaded object is new or not.
        /// </param>
        internal WorkObject(TDomainObject domainObject, bool isNew)
        {
            this.domainObject = domainObject;
            this.isNew = isNew;
        }

        private readonly TDomainObject domainObject;
        private readonly TDomainObject dirtyDomainObject;
        private readonly bool isNew;

        /// <summary>
        /// Gets the associated domain object with this work.
        /// </summary>
        public TDomainObject DomainObject
        {
            get
            {
                return domainObject;
            }
        }

        internal TDomainObject DirtyDomainObject
        {
            get
            {
                return dirtyDomainObject;
            }
        }

        /// <summary>
        /// Gets a value which specifies that current business object has not changed its state
        /// after its retrieval from its source data store.
        /// </summary>
        public bool Dirty
        {
            get
            {
                return DomainObject.DateAdded != DomainObject.DateModified;
            }
        }

        /// <summary>
        /// Gets a flag providing that this object has been created within its unit of work or not.
        /// </summary>
        public bool New
        {
            get
            {
                return isNew;
            }
        }

        /// <summary>
        /// Gets a flag providing that this work has been marked for deletion.
        /// </summary>
        public bool Delete
        {
            get;
            set;
        }
    }
}